var organization = require('../model/organization');

exports.select = function(req, res, connection) {
	organization.select(connection, function(rows) {
		res.send(rows);
	});
};
//查询机构树
exports.getOrgTree = function(req, res, connection) {
	console.log("organization---getOrgTree");
	exports.checkData(connection, function(data) {
		// console.log(data);
		var orgList = [];
		var sendData = [];
		if (data.length == 1) {
			sendData.push({
				id: data.id,
				pid: data.parentId,
				text: data.orgName,
				children: []
			});
		}
		for (var i = 0; i < data.length; i++) {
			var org = data[i];
			// console.log("-----------" + i + "---------" + org);
			var nodeData = {
				id: org.id,
				pid: org.parentId,
				text: org.orgName,
				children: []
			};
			orgList.push(nodeData);
			for (var k = 0; k < orgList.length; k++) {
				if (org.parentId == orgList[k].id) {
					orgList[k].children.push(nodeData);
				} else if (org.id == orgList[k].pid) {
					nodeData.children.push(orgList[k]);
				}
			}
		}
		sendData.push(orgList[0]);
		// console.log("----------sendData-----------");
		// console.log(sendData);

		res.send(sendData);
	});

};
exports.checkData = function(connection, callback) {
	organization.select(connection, function(data) {
		if (data != null && data.length != 0) {
			callback(data);
		} else {
			var insertData = {
				orgName: "总部",
				parentId: -1,
				orgManager: 0,
				systemId: 0

			};
			organization.add(connection, insertData, function(rows) {
				var orgId = rows.insertId;
				organization.findById(connection, orgId, function(result) {
					callback(result);
				});
			});
		}
	});
};

//根据机构id得到机构的父机构
exports.getParents = function(req, res, connection) {
	var orgId = req.params['orgId'];
	// console.log("getParents--orgId = " + orgId);

	organization.findById(connection, orgId, function(result) {
		var orgList = [];
		// console.log("getParents--result:", result);
		if (result != null && result.length != 0) {
			orgList.push(result[0]);

			// console.log(result[0].parent);
			if (result[0].parentId != -1) {

				exports.getParentList(connection, result[0].parentId, orgList, function(data) {
					// console.log("=====orgParents=======data============");
					// console.log(data);
					res.send(data);
				});

			} else {
				// console.log("=====orgParents=======orgList============");
				// console.log(orgList);
				res.send(orgList);
			}
		}


	});
};
exports.getSysName = function(req, res, connection) {
	var orgId = req.params['orgId'];
	organization.findSysNameByOrgid(connection, orgId, function(result) {
			res.send(result[0]);
	});
};

exports.getParentList = function(connection, pid, orgList, callback) {
	// console.log("---------getParentList-------pid----");
	// console.log(pid);

	organization.findById(connection, pid, function(result) {
		if (result != null) {
			orgList.push(result[0]);
			if (result[0].parentId != -1) {
				exports.getParentList(connection, result[0].parentId, orgList, function(data) {
					// console.log("=====getParentList=======data=========");
					// console.log(data)
					callback(data);
				});
			} else {
				callback(orgList);
			}
		}
	});
};

exports.addOrg = function(req, res, connection) {
	var data = req.body;
	if (data.systemId == null || data.systemId == "") {
		data.systemId = 0;
	}
	if (data.orgManager == null || data.orgManager == "") {
		data.orgManager = 0;
	}

	// console.log("---controller--addOrg--data:");
	// console.log(data);
	organization.add(connection, data, function(rows) {
		if (rows != null && rows.length != 0) {
			// console.log("3333333333333");
			// console.log("controller--insertId:", rows.insertId);
			res.send("添加机构成功！");
		} else {
			// console.log("444444444444444");
			res.send("添加机构失败！");
		}
	});
};

/*
校验机构名是否重复
同一个父节点下的机构不允许重名

*/
exports.checkOrgName = function(req, res, connection) {
	// var orgData = req.params['orgData'] + '';
	var orgData = req.query.orgData;
	var type = req.query.type;
	var orgJson = JSON.parse(orgData);
	// var type = req.params['type'];
	// console.log("===checkOrgName=====type======" );
	// console.log(type);
	var sendResult = "true";
	organization.checkOrgName(connection, orgJson, function(result) {
		// console.log("------checkOrgName-----controller------");
		// console.log(result);
		if (result != null && result.length != 0) {
			if (type == "put") {
				if (result[0].id != orgJson.id) {
					sendResult = "false";
				}
			} else {
				sendResult = "false"
			}
		}
		res.send(sendResult);
	});

};

exports.updateOrg = function(req, res, connection) {
	var data = req.body;
	if (data.systemId == null || data.systemId == "") {
		data.systemId = 0;
	}
	if (data.orgManager == null || data.orgManager == "") {
		data.orgManager = 0;
	}
	// console.log("controller--updateOrg--data:", data);
	organization.update(connection, data, function(rows) {
		// console.log("controller--updateOrg--rows", rows);
		if (rows != null && rows.length != 0) {
			// console.log("11111111111");
			res.send("更新机构成功！");
		} else {
			// console.log("222222222222");
			res.send("更新机构失败！");
		}
	});
};

exports.selOrgById = function(req, res, connection) {
	var orgId = req.params['orgId'];
	var sendData = {};
	// console.log("controller ---orgId = " + orgId);

	organization.selOrgById(connection, orgId, function(result) {
		sendData.orgData = result[0];
		var orgList = [];

		orgList.push(result[0]);

		if (result[0].parentId != -1) {
			exports.getParentList(connection, result[0].parentId, orgList, function(data) {
				sendData.parentData = data;
				res.send(sendData);
			});

		} else {
			sendData.parentData = orgList;
			res.send(sendData);
		}
	});
};

//校验体系下是否有机构
exports.checkSystem = function(req, res, connection) {
	var sysid = req.params['sysid'];
	var sendResult = "true";
	organization.selOrgBySys(connection, sysid, function(rows) {
		if (rows != null && rows.length != 0) {
			sendResult = "false";
		}
		res.send(sendResult);
	});
};

/**
校验机构是否可以删除
1，机构下有子机构时不可以删除
2，机构如果被其他表引用则不可以删除不可以删除
**/
exports.checkOrg = function(req, res, connection) {
	var orgId = req.params['orgId'];
	var sendResult = "true";
	organization.selChild(connection, orgId, function(rows) {
		if (rows != null && rows.length != 0) {
			sendResult = "hasChild";
			res.send(sendResult);
		} else {
			//校验机构是否被引用
			organization.selEmployee(connection, orgId, function(results) {
				console.log("selEmployee--results", results);
				if (results != null && results.length != 0) {
					sendResult = "hasEmp";
				}
				res.send(sendResult);
			});
		}
		// res.send(sendResult);
	});
};

exports.delOrg = function(req, res, connection) {
	var orgId = req.query.orgId;
	organization.delet(connection, orgId, function(result) {
		res.send(result);
	});
};

//获取所有的部门信息
exports.getDepart = function(req, res, connection) {
	organization.selOrgIsDepart(connection, function(rows) {
		res.send(rows);
	});
};

//查询单个部门信息
exports.selectOne = function(req, res, connection) {
	organization.findById(connection, function(rows) {
		res.send(rows);
	});
};

exports.selectOneOrg = function(req, res, connection) {
	var id = req.params['id'];
	organization.findById(connection, id, function(rows) {
		res.send(rows);
	});
};

//校验是否存在根节点
exports.checkRoot = function(req, res, connection) {
	organization.selRoot(connection, function(rows) {
		res.send(rows);
	});
};
//根据当前用户查询负责机构
exports.selOrgByUser = function(req, res, connection) {
	var uid = req.user.id;
	organization.selOrgId(connection, uid, function(rows) {
		// console.log('根据当前用户查询负责机构:', rows);
		res.send(rows);
	});
};